package qlsl.androiddesign.http.service.subservice;

import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import qlsl.androiddesign.constant.WhatConstant;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.ShoppingDetail;
import qlsl.androiddesign.handler.subhandler.HttpHandler;
import qlsl.androiddesign.http.HttpListener;
import qlsl.androiddesign.http.service.baseservice.BaseService;
import qlsl.androiddesign.http.xutils.HttpProtocol;
import qlsl.androiddesign.thread.HandlerThread;
import qlsl.androiddesign.util.commonutil.PagerUtils;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 商品列表模块 <br/>
 * 
 */
public class ShoppingListService extends BaseService {

	private static String className = getClassName(ShoppingListService.class);

	/**
	 * 查询商品列表
	 * 
	 * @param userid
	 *            用户的id
	 * @param sumcierid
	 *            三级分类ID
	 * @param orderby
	 *            排序方式：0默认1销量2价格
	 * @param functionView
	 * @param listener
	 */
	public static void queryShoopingList(final String userid,
			final String sumcierid, final String orderby,
			final FunctionView<?> functionView, final HttpListener listener) {

		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryShoopingList");
		new HandlerThread(className, "queryShoopingList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getgoodslist")
							.addParam("sumcierid", sumcierid)
							.addParam("orderby", orderby).post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					List<ShoppingDetail> list = JSONArray.parseArray(
							jo.getString("rows"), ShoppingDetail.class);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, list);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 查询我的商品
	 */
	public static void queryShoopingList(final FunctionView<?> functionView,
			final HttpListener listener) {
		functionView.setProgressBarText("正在查询我的商品");
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryShoopingList");
		new HandlerThread(className, "queryShoopingList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getgoodslist")
							.addParam("self", 1).addParam("orderby", 0).post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					List<ShoppingDetail> list = JSONArray.parseArray(
							jo.getString("rows"), ShoppingDetail.class);

					HashMap<String, Object> map = new HashMap<String, Object>();
					Pager pager = PagerUtils.createPager(1, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, map);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 下架商品
	 */
	public static void deleteGood(final String goodsid,
			final FunctionView<?> functionView, final HttpListener listener) {
		functionView.setProgressBarText("正在请求");
		final HttpHandler handler = getHandler(functionView, listener,
				className, "deleteGood");
		new HandlerThread(className, "deleteGood") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("delgoods")
							.addParam("goodsid", goodsid).post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, "成功");
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}
}
